import 'package:bilibili_getx_project/core/I18n/str_res_keys.dart';
import 'package:bilibili_getx_project/core/service/request/home_requests.dart';
import 'package:bilibili_getx_project/ui/shared/app_theme.dart';
import 'package:bilibili_getx_project/ui/shared/asset_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import 'home_state.dart';

class HomeLogic extends GetxController {
  final HomeState state = HomeState();

  @override
  void onReady() {
    if (!state.teenagerMode) {
      showTeenagerModeDialog();
    }
    fetchSearchSquare();
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  void fetchSearchSquare() {
    Map<String, String> params = {
      'appkey': '1d8b6e7d45233436',
      'build': '7110300',
      'c_locale': 'zh_CN',
      'channel': 'bili',
      'disable_rcmd': '0',
      'from': '0',
      'limit': '10',
      'mobi_app': 'android',
      'platform': 'android',
      's_locale': 'zh_CN',
      'show': '0',
      'statistics':
          '%7B%22appId%22%3A1%2C%22platform%22%3A3%2C%22version%22%3A%227.11.0%22%2C%22abtest%22%3A%22%22%7D',
      'ts': '1672272837',
      // 'sign': '9d68cfe8ad480bc03cd86e174340c9bd',
    };
    HomeRequest.fetchSearchSquareData(params).then((value) {
      if(value.code == 0) {
        state.firstSearchKey = value.data!.last.data!.list!.first.title!;
        update();
      }
    });
  }

  void showTeenagerModeDialog() {
    TextStyle greyTextStyle =
        TextStyle(color: AppTheme.greyColor, fontSize: 12.sp);
    SmartDialog.show(
      builder: (ctx) {
        return Card(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(5.r),
            ),
          ),
          child: Container(
            padding: EdgeInsets.all(10.r),
            width: 225.w,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                10.verticalSpace,
                Image.asset(
                  AssetsImage.teenagerModePNG,
                  width: 50.r,
                  height: 50.r,
                ),
                10.verticalSpace,
                Text(
                  SR.teenagerModeDesc.tr,
                  style: greyTextStyle,
                ),
                10.verticalSpace,
                Text(SR.teenagerModeButtonText.tr, style: greyTextStyle),
                10.verticalSpace,
                GestureDetector(
                  onTap: () {
                    SmartDialog.dismiss();
                  },
                  child: Container(
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(3.r)),
                        border:
                            Border.all(color: AppTheme.greyColor, width: .5)),
                    padding: EdgeInsets.symmetric(vertical: 5.r),
                    width: 200.w,
                    child: Text(
                      SR.iKnowIt.tr,
                      style:
                          TextStyle(color: AppTheme.norBlack, fontSize: 12.sp),
                    ),
                  ),
                ),
                5.verticalSpace,
              ],
            ),
          ),
        );
      },
      clickMaskDismiss: false,
    );
  }
}
